
var detailupdate = require('../../api/update.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: 'Leech',
    type1: 'robot',
    type2: 'instructions',//baseinfo 基本信息  //instructions 编辑描述信息 //detailMK1  MK1机体信息  //detailMK2  MK2机体信息  //SpecialMK1  MK1技能信息 //SpecialMK2  MK2技能信息  
    type2cn: 'mk2机体信息修改',
    editobj: null
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(
      { id: options.id }
    )
    this.setData(
      { type1: options.type1 }
    )
    this.setData(
      { type2: options.type2 }
    )
    this.setData(
      { type2cn: options.type2cn }
    )
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    const db = wx.cloud.database()
    // 查询当前用户所有的 counters
    try {
      db.collection('wrobot').where({
        id: that.data.id
      }).get({
        success: res => {
          var detailobj = res.data[0];
          detailobj = JSON.parse(detailobj.data);
          if (detailobj == null) {
            wx.showToast({
              title: '读取数据错误，请退出重新尝试获取数据',
              icon: 'none',
              duration: 10000
            })
            return;
          }
          if (this.data.type2 == "instructions") {
            if (detailobj.instructions == "即将推出...") {
              detailobj.instructions = [{ "Ins": "▷即将推出..." }];
            }
          }
          this.setData({
            editobj: detailobj
          })

          wx.hideToast();
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '查询记录失败'
          })
          wx.hideToast();
        }
      })
    } catch (e) {
      wx.showToast({
        title: '读取数据失败，请检查网络',
        icon: 'none',
        duration: 10000
      })
    }
  },
  updatedata: function (e) {
    var yep = `editobj.${e.currentTarget.dataset.key}`
    this.setData({ [yep]: e.detail.value })
  },
  updateinstructions: function (e) {
    var yep = `editobj.${e.currentTarget.dataset.key}[${e.currentTarget.dataset.index}].Ins`
    this.setData({ [yep]: e.detail.value })
  },
  updatelistdata: function (e) {
    var yep = `editobj.${this.data.type2}[${e.currentTarget.dataset.index}].${e.currentTarget.dataset.key}`
    // if (e.detail.value == "") {
    //   e.detail.value = "-";
    //   this.setData({ [yep]: "-" });
    //   return;
    // }
    this.setData({ [yep]: e.detail.value })
  },
  addins: function () {
    var instructions = this.data.editobj.instructions;
    instructions.push({ "Ins": "▷" });
    this.setData({ 'editobj.instructions': instructions })
  },
  removeins: function () {
    var instructions = this.data.editobj.instructions;
    instructions.pop();
    this.setData({ 'editobj.instructions': instructions })
  },
  showToast: function (title, icon){
    wx.showToast({
      title: title,
      icon: icon,
      duration: 3000
    })
  },
  updatesave: function (uinfo) {
    if(!uinfo){
      wx.showToast({
        title: '获取用户信息失败',
        icon: 'none',
        duration: 3000
      })
    }
    //63eab1738b829d0938a009e474ec05b7fed2942d
    detailupdate.updatedetail(this, uinfo.detail.userInfo);
  }
})

